package com.aliyun.timetask;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.aliyun.bean.ResultAddress;
import com.aliyun.service.YuMingService;
import com.aliyun.util.HttpClientUtil;

/**
 */
public class YuMingCheckTask {

	@Resource(name = "yumingService")
	private YuMingService yumingService;

	public void beginTask() {
		try {
			Map<String, Object> map = yumingService.getYuMingItemOne();
			if(map!=null && map.size()>0){
				//YuMingCheckThread ymThread = new YuMingCheckThread(map);
				//ymThread.start();
				HttpClientUtil hcUtil = HttpClientUtil.getInstance();
				ResultAddress ra = hcUtil.checkInetAddressS(map);
				String status = ra.getStatus();
				Map<String,String> updateMap = new HashMap<String,String>();
				updateMap.put("status", status);
				updateMap.put("currentaddress", "".equals(ra.getLocation())?map.get("name")+"":ra.getLocation());
				updateMap.put("name", map.get("name")+"");
				
				if(!"".equals(ra.getLocation())&&(ra.getLocation().startsWith("http://"+map.get("name")+"")
						||ra.getLocation().startsWith("https://"+map.get("name")+"")
						||ra.getLocation().contains(map.get("name")+""))){
					updateMap.put("status", "1");
					updateMap.put("currentaddress", ra.getLocation().split("/robot")[0]);
					
				}
				
				try {
					yumingService.updateYuMingStatus(updateMap);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
